package com.yunban.project.template.controller;

import com.yunban.project.template.model.domain.TemplateGroup;
import com.yunban.project.template.model.dto.*;
import com.yunban.project.template.model.vo.TemplateGroupResp;
import com.yunban.project.template.model.vo.TemplateResp;
import com.yunban.project.template.service.TemplateCollectService;
import com.yunban.project.template.service.TemplateGroupMapService;
import com.yunban.project.template.service.TemplateGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import net.lab1024.sa.base.common.annoation.NoNeedLogin;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@OperateLog
@RestController
@Tag(name = "模板组管理")
@RequestMapping("/template-group-api")
public class TemplateGroupController {

    @Resource
    private TemplateGroupService templateGroupService;

    @Resource
    private TemplateGroupMapService templateGroupMapService;

    @Resource
    private TemplateCollectService templateCollectService;

    @PostMapping("/list")
    @Operation(summary = "获取模板组列表")
    PageResult<TemplateGroupResp> getTemplateGroupList(@RequestBody TemplateGroupSearchReq req) {
        return this.templateGroupService.getTemplateGroupList(req);
    }

    @NoNeedLogin
    @GetMapping("/{groupId}")
    @Operation(summary = "获取模板组详情")
    ResponseDTO<String> getTemplateGroupHtml(@PathVariable long groupId) {
        TemplateGroup templateGroup = this.templateGroupService.getById(groupId);
        return ResponseDTO.ok(templateGroup.getHtml());
    }

    @PostMapping
    @Operation(summary = "新增模板组")
    Boolean saveTemplateGroup(@RequestBody @Validated TemplateGroupReq req) {
        return this.templateGroupService.saveTemplateGroup(req);
    }

    @PutMapping("/{id}")
    @Operation(summary = "编辑模板组")
    Boolean editTemplateGroup(@PathVariable long id, @RequestBody @Validated TemplateGroupReq req) {
        return this.templateGroupService.editTemplateGroup(id, req);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除模板组")
    Boolean removeTemplateGroup(@PathVariable long id) {
        return this.templateGroupService.removeTemplateGroup(id);
    }

    @PostMapping("/save-mapping/{groupId}")
    @Operation(summary = "添加某个模板到某个组中")
    Boolean addTemplateToGroup(@PathVariable long groupId, @RequestBody @Validated AddTemplateToGroupReq req) {
        return this.templateGroupMapService.addTemplateToGroup(groupId, req);
    }

    @PostMapping("/batch-save/{groupId}/mapping")
    @Operation(summary = "批量添加模板到某个组中")
    Boolean batchSaveTemplateToGroup(@PathVariable long groupId, @RequestBody @Validated BatchSaveTemplateToGroupReq req) {
        return this.templateGroupMapService.batchSaveTemplateToGroup(groupId, req);
    }

    @PostMapping("/remove/mapping")
    @Operation(summary = "删除模板组中的某个模板")
    Boolean removeTemplateGroupMap(@RequestBody @Validated RemoveTemplateGroupMapReq req) {
        return this.templateGroupMapService.removeTemplateGroupMap(req);
    }

    @GetMapping("/{groupId}/mapping/list")
    @Operation(summary = "获取某个模板组下的模板列表")
    List<TemplateResp> findByGroupId(@PathVariable long groupId) {
        return this.templateGroupMapService.findByGroupId(groupId);
    }

    @Operation(summary = "收藏模板组")
    @PostMapping("/{templateGroupId}/collect")
    Boolean collect(@PathVariable long templateGroupId) {
        return templateCollectService.collect(templateGroupId);
    }

    @Operation(summary = "取消收藏组")
    @PostMapping("/{groupId}/remove/collect")
    Boolean removeCollect(@PathVariable long groupId) {
        return templateCollectService.removeCollect(groupId);
    }

    // 获取我已收藏的模板组
    @GetMapping("/collect/list")
    @Operation(summary = "获取我已收藏的模板组")
    List<TemplateGroupResp> getUserCollectList() {
        // 获取我的所有收藏记录ID
        List<Long> groupIds = this.templateCollectService.getUserCollectList(SmartRequestUtil.getUserId());
        List<TemplateGroup> templateGroups = this.templateGroupService.listByIds(groupIds);
        return SmartPageUtil.convert(templateGroups, TemplateGroupResp.class);
    }
}
